package com.bauer.base.designpattern.chainofresponsibility;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ：rui.wei
 * @Date ：Created in 15:28 2019/7/8
 * @Description： 责任链模式是
 *
 * FilterChain 也实现了MsgFilter  好处就是   整个FilterChain也可以当做一个链条添加到  上一个filterchain中
 */
public class Main {

    public static void main(String[] args) {
        FilterChain fc1 = new FilterChain();
        FilterChain fc2 = new FilterChain();
        fc1.add(new HtmlFilter()).add(new SQLFilter()).add(fc2);
    }

}


interface MsgFilter {

    boolean doFilter(String msg);
}


class HtmlFilter implements MsgFilter {

    @Override
    public boolean doFilter(String msg) {
        return true;
    }
}

class SQLFilter implements MsgFilter {

    @Override
    public boolean doFilter(String msg) {
        return true;
    }
}

class FilterChain implements MsgFilter {

    private List<MsgFilter> filters = new ArrayList<>();

    public FilterChain add(MsgFilter msgFilter) {
        filters.add(msgFilter);
        return this;
    }

    @Override
    public boolean doFilter(String msg) {
        for (MsgFilter msgFilter : filters) {
            if (!msgFilter.doFilter(msg)) {
                return false;
            }
        }
        return true;
    }
}